package com.huntero.hweather.bean;

import java.io.File;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

/**
 * {"c": {"c1":"101010100", "c2":"beijing", "c3":"北京", "c4":"beijing",
 * "c5":"北京", "c6":"beijing", "c7":"北京", "c8":"china", "c9":"中国", "c10":"1",
 * "c11":"010", "c12":"100000", "c13":"116.407526", "c14":"39.904030",
 * "c15":"33", "c16":"AZ9010", "c17":"+8"},
 * 
 * "f": {"f1":
 * [{"fa":"00","fb":"01","fc":"34","fd":"21","fe":"0","ff":"0","fg":"0"
 * ,"fh":"0","fi":"04:45|19:43"},
 * {"fa":"01","fb":"01","fc":"32","fd":"22","fe":"0"
 * ,"ff":"0","fg":"0","fh":"0","fi":"04:45|19:43"},
 * {"fa":"01","fb":"02","fc":"32"
 * ,"fd":"22","fe":"0","ff":"0","fg":"0","fh":"0","fi":"04:45|19:44"}],
 * "f0":"201406121100"}}
 * 
 * @author Huntero
 *
 */
public class Weather {

	public String cityCode; // c.c1
	public String cityName; // c.c3
	public WeatherInfo[] infos; // f.f1 数组
	private String publishTime; // f.f0

	public Weather() {
	}

	public Weather(String json) {
		readFromJson(json);
	}

	/**
	 * @param json
	 */
	private void readFromJson(String json) {
		if (TextUtils.isEmpty(json))
			return;
		try {
			JSONObject jsonObj = new JSONObject(json);
			JSONObject c = jsonObj.getJSONObject("c");
			this.cityCode = c.getString("c1");
			this.cityName = c.getString("c3");

			JSONObject f = jsonObj.getJSONObject("f");
			this.publishTime = f.getString("f0");

			JSONArray f1 = f.getJSONArray("f1");
			int len = f1.length();
			this.infos = new WeatherInfo[len];
			WeatherInfo wi = null;
			JSONObject _f1 = null;
			for (int i = 0; i < len; i++) {
				_f1 = f1.getJSONObject(i);

				wi = new WeatherInfo();
				wi.dayWeatherCode = _f1.getString("fa");
				wi.nightWeatherCode = _f1.getString("fb");
				wi.dayTemp = _f1.getString("fc");
				wi.nightTemp = _f1.getString("fd");
				wi.dayFindyCode = _f1.getString("fe");
				wi.nightFindyCode = _f1.getString("ff");
				wi.dayFindyStrong = _f1.getString("fg");
				wi.nightFindyStrong = _f1.getString("fh");
				String sun = _f1.getString("fi");
				String[] suns = sun.split("\\|");
				wi.sunUpTime = suns[0];
				wi.sunDownTime = suns[1];

				this.infos[i] = wi;
			}

		} catch (JSONException e) {
			Log.e("Weather", "read from json exception", e);
		}
	}

	public class WeatherInfo {
		protected String dayWeatherCode; // f.f1[0].fa
		protected String nightWeatherCode; // f.f1[0].fb
		public String dayTemp; // f.f1[0].fc
		public String nightTemp; // f.f1[0].fd
		protected String dayFindyCode; // f.f1[0].fe
		protected String nightFindyCode; // f.f1[0].ff
		protected String dayFindyStrong; // f.f1[0].fg
		protected String nightFindyStrong; // f.f1[0].fh
		public String sunUpTime; // f.f1[0].fi[0]
		public String sunDownTime; // f.f1[0].fi[1]

		public Uri getWeatherImageUri() {
			String uriStr = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hweather/";
			if (!isDayTime()
					&& ("00".equals(nightWeatherCode)
							|| "01".equals(nightWeatherCode)
							|| "03".equals(nightWeatherCode) || "13"
								.equals(nightWeatherCode))) {
				// Tonight
				uriStr += "/night/" + this.nightWeatherCode
						+ ".png";

			} else {
				uriStr += "/day/" + this.dayWeatherCode
						+ ".png";
			}
			return Uri.parse(uriStr);
		}
	}

	/* 是否上午 */
	protected boolean isBeforeNoon() {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		return hour < 12 ? true : false;
	}

	/* 是否白天 */
	protected boolean isDayTime() {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		return hour < 18 ? true : false;
	}

	public String getPublishTime() {
		if (TextUtils.isEmpty(publishTime) || this.publishTime.length() != 12)
			return null;
		String time = this.publishTime.substring(8);
		char[] chars = time.toCharArray();
		char[] c = new char[5];
		System.arraycopy(chars, 0, c, 0, 2);
		c[2] = ':';
		System.arraycopy(chars, 2, c, 3, 2);
		return String.valueOf(c);
	}

	@Override
	public String toString() {
		return "Weather [cityCode=" + cityCode + ", cityName=" + cityName
				+ ", publishTime=" + publishTime + "]";
	}

}
